package com.xiaoran.RMQ_1.test8_over_channel;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.xiaoran.RMQ_1.utlis.RabbitMqUtils;

import java.util.HashMap;
import java.util.Map;

public class Over_channel_Conmuner_1 {
    public static final String  normal_Exchange="normal_Exchange";
    public static final String  normal_queue="normal_queue";
    public static final String dead_Exchange="dead_Exchange";
    public static final String  dead_queue="dead_queue";
    public  static void main(String args[]) throws  Exception{
        System.out.println("Conmuner_1等待接收消息。。。。。");
        Channel channel=RabbitMqUtils.getChannel();

        channel.exchangeDeclare(normal_Exchange,BuiltinExchangeType.DIRECT);
        channel.exchangeDeclare(dead_Exchange,BuiltinExchangeType.DIRECT);
        //普通队列
        Map<String, Object> arguments=new HashMap<>();
        //设置过期时间,可以在生产者中设置也可以在消费者中设置
        //arguments.put("x-message-ttl",100000);
        //正常队列设置死信交换机是谁
        arguments.put("x-dead-letter-exchange",dead_Exchange);
        //声明死信队列的routingKey
        arguments.put("x-dead-letter-routing-key","lisi");
        //设置正常队列的长度限制
        //arguments.put("x-max-length",6);
        channel.queueDeclare(normal_queue,false,false,false,arguments);
        //死信队列

        channel.queueDeclare(dead_queue,false,false,false,null);

        channel.queueBind(normal_queue,normal_Exchange,"zhangshan");
        channel.queueBind(dead_queue,dead_Exchange,"lisi");

        DeliverCallback deliverCallback=(consumerTag,message)->{
            String msg=new String(message.getBody());
            if (msg.equals("info5")){
                System.out.println("接收到被拒绝的消息是："+msg);
                channel.basicReject(message.getEnvelope().getDeliveryTag(),false);
            }else{
                System.out.println("Conmuner_1收到的消息是："+msg);
                channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
            }
        };
        CancelCallback cancelCallback=consumerTag->{ };
        channel.basicConsume(normal_queue,false,deliverCallback,cancelCallback);
    }
}
